package com.fancon.android.gardenjokesx.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.adapter.FriendsAdapter;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.FanconProgressBar;

public class S042Activity extends GardenJokesBaseActivity{
	private ArrayList<User> listUser;
	private FriendsAdapter friendAdapter;
	private ListView listFriends;
	private EditText mEditSearch;
	private ImageView mImgSearch;
	String USER_ID = "";
	private String result = "";
//	private Boolean toogle = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_friend); 
		User user = UserUtil.getInfoUserLogin(getApplicationContext());
		if(user != null){
			USER_ID = user.getUser_id();
		}
		TextView tvTitleListFriends = (TextView) findViewById(R.id.common_tv_title_list_friends);
		tvTitleListFriends.setText(getString(R.string.s04_2_tv_title_list_friends));
		mEditSearch = (EditText) findViewById(R.id.common_edit_search_friends);
		mImgSearch = (ImageView)findViewById(R.id.s042_img_search);
		listFriends = (ListView) findViewById(R.id.common_item_list_friends);
		listUser = new ArrayList<User>();
		SharedPreferences settings = getSharedPreferences(Constant.PREFS_NAME, 0);
		mEditSearch.setText(settings.getString("keyword", null));	
		mEditSearch.requestFocus();
		InputMethodManager inputManager = null;
		inputManager = (InputMethodManager)            
		S042Activity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
		inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 1);
//		toogle = true;
		/*
		 * Click search on keyboard
		 */
		mEditSearch.setOnKeyListener(new OnKeyListener()
		{
		    public boolean onKey(View v, int keyCode, KeyEvent event)
		    {
		            if(keyCode == KeyEvent.KEYCODE_ENTER){
		            	Log.d("click enter", "enter");
		            	if(mEditSearch.getText().toString().length() > 0){
							ListFriendRequest request = new ListFriendRequest("S042", S042Activity.this);
							getGlobalState().getRequestQueue().addRequest(request);
						}
	                    return true;
		            }
		        return false;
		    }
		});
		mEditSearch.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Log.d("s042", "touch vao search!");
				InputMethodManager inputManager = null;
				inputManager = (InputMethodManager)            
				S042Activity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 1);
//				toogle = true;
				return false;
			}
		});
		/*
		 * Click search on image search
		 */
		mImgSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mEditSearch.getText().toString().length() > 0){
					ListFriendRequest request = new ListFriendRequest("S042", S042Activity.this);
					getGlobalState().getRequestQueue().addRequest(request);
				}
			}
		});
	}
	class ListFriendRequest extends RequestUI {

		private Activity context;
		private FanconProgressBar mProgressDialog;
		public ListFriendRequest(Object key, Activity activity) {
			super(key, activity);
			context = activity;
			mProgressDialog = FanconProgressBar.show(context,getString(R.string.common_loading));
		}

		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			Log.d("search name", "exec API");
			//@binhbt-comment
			getListFriend(context, mEditSearch.getText().toString(), USER_ID);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			if(!result.equals("")){
			friendAdapter = new FriendsAdapter(S042Activity.this, listUser, USER_ID);
			Log.d("search name", "set adapter");
			listFriends.setAdapter(friendAdapter);
			SharedPreferences settings = getSharedPreferences(Constant.PREFS_NAME, 0);
			SharedPreferences.Editor edit = settings.edit();
			edit.putString("keyword", mEditSearch.getText().toString());
			edit.commit();
			}
			InputMethodManager inputManager = null;
			 inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
			 inputManager.toggleSoftInput(1, 0);
			mProgressDialog.dismiss();
		}
	}
	
	private void getListFriend(Activity context, String keyword, String user_id) {
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("keyword", keyword));
		nameValuePairs.add(new BasicNameValuePair("user_id", user_id));
		GardenJokesApiConnect api = new GardenJokesApiConnect(context);
		result = api.execGet(getApplicationContext(), GardenJokesApiConnect.GROUP_S, "getListUserByUserName", nameValuePairs);
		Log.d("json list friend", result);
		JsonPaser json = new JsonPaser();
		listUser = json.getListUser(result);
	}

}
